package cn.soft863.bike.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.soft863.bike.dao.IABikeDao;
import cn.soft863.bike.dao.IACateDao;
import cn.soft863.bike.dao.impl.ABikeDaoImpl;
import cn.soft863.bike.dao.impl.ACateDaoImpl;
import cn.soft863.bike.pojo.ABike;
import cn.soft863.bike.pojo.ACate;
import cn.soft863.bike.service.IBikeService;
import cn.soft863.common.util.PageInfoUtil;

/**
 * 车辆信息的Service
 * 
 * @author 902-B-Book
 *
 */
public class BikeServiceImpl implements IBikeService
{
	/**
	 * 持有Dao的接口,如何赋值
	 */
	private IABikeDao bikeDao = new ABikeDaoImpl();
	private IACateDao cateDao = new ACateDaoImpl();

	@Override
	public int saveOneBikeService(ABike bike)
	{
		return this.bikeDao.saveOneDao(bike);
	}

	@Override
	public List<ABike> findCondListBikeService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<ABike> bikeList = this.bikeDao.findCondListDao(pageInfoUtil, condMap);
		/* 查询关联对象的信息 */
		if("true".equalsIgnoreCase(condMap.get("extends") + ""))
		{
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			/* 查询车辆相关的分类 */
			for (Iterator iterator = bikeList.iterator(); iterator.hasNext();)
			{
				ABike bike = (ABike) iterator.next();
				
				/* 查询分类 
				 * 根据分类的id查询
				 * */
				extMap.put("id", bike.getCateId());
				ACate cate = this.findOneCateService(extMap);
				bike.setCate(cate);
			}
		}
		return bikeList ; 
	}

	@Override
	public ABike findOneBikeService(Map<String, Object> condMap)
	{
		ABike bike =  this.bikeDao.findOneDao(condMap);
		/* 查询关联对象的信息 */
		if("true".equalsIgnoreCase(condMap.get("extends") + ""))
		{
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			/* 查询分类 
			 * 根据分类的id查询
			 * */
			extMap.put("id", bike.getCateId());
			ACate cate = this.findOneCateService(extMap);
			bike.setCate(cate);
		}
		return bike ; 
	}

	@Override
	public int updateOneBikeService(ABike bike)
	{
		return this.bikeDao.updateOneDao(bike);
	}

	@Override
	public int saveOneCateService(ACate cate)
	{
		return this.cateDao.saveOneDao(cate);
	}

	@Override
	public List<ACate> findCondListCateService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		return this.cateDao.findCondListDao(pageInfoUtil, condMap);
	}

	@Override
	public ACate findOneCateService(Map<String, Object> condMap)
	{
		return this.cateDao.findOneDao(condMap);
	}

	@Override
	public int updateOneCateService(ACate cate)
	{
		return this.cateDao.updateOneDao(cate);
	}
}
